import 'package:color_dart/color_dart.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk_example/components/a_button/index.dart';
import 'package:im_flutter_sdk_example/components/empty_list.dart';
import 'package:im_flutter_sdk_example/components/labels/index.dart';
import 'package:im_flutter_sdk_example/jsonserialize/group/data.dart';
import 'package:im_flutter_sdk_example/jsonserialize/group/group_apply.dart';
import 'package:im_flutter_sdk_example/utils/constants.dart';
import 'package:im_flutter_sdk_example/utils/global.dart';

class AuditItem extends StatelessWidget {
  final GroupApplyModel item;
  final Function callbacks;

  const AuditItem({Key key, this.item, this.callbacks}) : super(key: key);

  void approveApply() async {
    print(item.groupId);
    var res = await G.req.group
        .approveApply(groupId: item.groupId, applierId: item.applierId);
    print(res);
  }

  void rejectApply() async {
    print(item.groupId);
    G.showLoading();
    await G.req.group
        .rejectApply(groupId: item.groupId, applierId: item.applierId);
    G.dismissLoading();
    print('xxx $callbacks');
    if (callbacks != null) {
      callbacks();
      print('刷新申请列表');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 10),
      child: ListTile(
          title: Container(
            child: Row(
              children: [
                Padding(
                  padding: const EdgeInsets.symmetric(vertical: 6),
                  child: Container(
                      width: 150,
                      child: Text(
                        '${item.userInfo.nickname}',
                        style: mainTitleStyle,
                        overflow: TextOverflow.ellipsis,
                      )),
                ),
                Container(
                    margin: EdgeInsets.only(left: 10),
                    child: Row(children: [LabelExpert()]))
              ],
            ),
          ),
          subtitle: Text('申请加入到您的小组，请完成审核！',
              style: TextStyle(color: rgba(136, 135, 135, 1), fontSize: 13)),
          leading: SizedBox(
            width: 60,
            height: 60,
            child: ClipOval(
              child: Image.asset(
                'lib/assets/images/order/order1.png',
                width: 30,
                height: 30,
                fit: BoxFit.cover,
              ),
            ),
          ),
          trailing: Column(
            children: [
              Padding(
                padding: EdgeInsets.only(bottom: 6),
                child: AButton.normal(
                    onPressed: () => {approveApply()},
                    bgColor: rgba(255, 255, 255, 1),
                    width: 55,
                    height: 25,
                    borderColor: rgba(47, 128, 250, 1),
                    plain: true,
                    borderRadius: BorderRadius.circular(12),
                    color: rgba(42, 125, 250, 1),
                    child: Text('通过')),
              ),
              AButton.normal(
                  onPressed: () => {rejectApply()},
                  bgColor: rgba(255, 255, 255, 1),
                  width: 55,
                  height: 25,
                  borderColor: rgba(216, 216, 216, 1),
                  plain: true,
                  borderRadius: BorderRadius.circular(12),
                  color: rgba(102, 102, 102, 1),
                  child: Text('拒绝')),
            ],
          )),
    );
  }
}

class AuditList extends StatefulWidget {
  const AuditList({Key key}) : super(key: key);

  @override
  _AuditListState createState() => _AuditListState();
}

class _AuditListState extends State<AuditList> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('申请列表')),
      body: listData.length == 0
          ? EmptyList()
          : Container(
              color: rgba(255, 255, 255, 1),
              child: Column(
                children: [
                  Column(
                    children: listData
                        .map((item) => AuditItem(
                              item: item,
                              callbacks: getData,
                            ))
                        .toList(),
                  )
                ],
              ),
            ),
    );
  }

  List<GroupApplyModel> listData = [];
  List<Group> myGroup = [];
  @override
  void initState() {
    super.initState();
    this.getData();
  }

  void getData() async {
    // 获取我的小组 Group
    var myGroupList = await G.req.group.groupMy();
    if (myGroupList.data['status'] == 1) {
      myGroupList.data['data'].forEach((e) {
        Group list = Group.fromJson(e);
        myGroup.add(list);
      });
    }
    if (myGroup.length != 0) {
      Group group = myGroup[0];
      var res = await G.req.group.applyList(groupId: "${group.id}");
      res.data['data'].forEach((e) {
        GroupApplyModel list = GroupApplyModel.fromJson(e);
        listData.add(list);
      });
      this.setState(() {});
    }
  }
}
